





//
//  ZBaseViewController.m
//  ZQLearn
//
//  Created by 朵 on 2019/4/16.
//  Copyright © 2019年 admin. All rights reserved.
//
#import "ZLoginViewController.h"
#import "ZBaseViewController.h"
#import "UIView+Sizes.h"
@interface ZBaseViewController ()

@property (nonatomic, strong) UIImageView *tipImage;
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation ZBaseViewController

///返回YES同时识别多个手势

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackgroundColor;
    [self setupNavigationBar];
    
    if (IOS7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
    }
    
    // Do any additional setup after loading the view.
}

- (void)setupNavigationBar
{
    UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 26, 45);
    
    //文字
    //    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    backBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    //如果文字喝图片同时存在就把这个调整距离的打开
    //    [backBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    //图像
    [backBtn setImage:[UIImage imageNamed:@"whiteback"] forState:UIControlStateNormal];
    
    [backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
    [backBtn addTarget:self action:@selector(backItemClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    
    
//    self.navigationController.navigationBar.translucent = NO;
    
    //    self.navigationController.navigationBar.barTintColor = kNavigationBarGrayColor;
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    //    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"导航栏背景"] forBarMetrics:UIBarMetricsDefault];
    //    self.navigationController.navigationBar.shadowImage = [UIImage imageNamed:@"下外发光"];
    
}

-(UIButton *)addNavigationLeftButtonFrame:(CGRect)frame Image:(UIImage *)normalImage AndHighLightImage:(UIImage *)highLightImage AndText:(NSString *)text Target:(id)target Action:(SEL)action
{
    
    UIButton *leftBtn                        = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame                  = frame;
    leftBtn.titleLabel.font        = kFont14;
    [leftBtn setImage:normalImage forState:UIControlStateNormal];
    [leftBtn setImage:highLightImage forState:UIControlStateHighlighted];
    [leftBtn setTitle:text forState:UIControlStateNormal];
    [leftBtn setTitle:text forState:UIControlStateHighlighted];
    [leftBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    [leftBtn setTitleColor:kWhiteColor forState:UIControlStateHighlighted];
    [leftBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    
    UIBarButtonItem *leftButtonItem       = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
    
    //    UIBarButtonItem * space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //    space.width = -4;
    //    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:space,leftButtonItem, nil];
    
    self.leftBarButton = leftBtn;
    
    return leftBtn;
}

-(UIButton *)addNavgationRightButtonWithFrame:(CGRect)frame
                                        title:(NSString*)title
                                        Image:(NSString*)image
                                  selectedIMG:(NSString*)imsel
                                      tartget:(id)target
                                       action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame =frame;
    ///3s内不允许重复点击
//    button.uxy_acceptEventInterval = 3.f;
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = kFont16;
    //    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:kNavigationTitleColor forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    if (imsel) {
        [button setImage:[UIImage imageNamed:imsel] forState:UIControlStateSelected];
    }
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    self.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    //    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    //button.frame = CGRectMake(0, 0, 40, 30);
    self.navigationItem.rightBarButtonItem = self.rightBarButtonItem;
    return button;
}

- (void)backItemClick
{
    //解决视图上移直接点返回造成的view上移下不来的情况
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    NSArray *viewcontrollers=self.navigationController.viewControllers;
    
    if (viewcontrollers.count>1) {
        
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) {
            
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else{
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
    
}




///调起登录页面
-(void)callLoginVC{
    
    ZLoginViewController *login = [[ZLoginViewController alloc]initWithNibName:@"ZLoginViewController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:login];
    //解决present延迟弹出
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:nav animated:YES completion:^{
            
        }];
    });
}


///数据为空时候显示
- (void)showNoDataImageView:(NSString *)imageName withTitle:(NSString *)title{
    self.tipImage.image = WDImgName(imageName);
    self.nameLabel.text = title;
    [self.view addSubview:self.nameLabel];
    [self.view addSubview:self.tipImage];
}

///数据存在时
- (void)hideNilImageWhenHasData {
    
    if (self.tipImage) {
        [self.tipImage removeFromSuperview];
    }
    if (self.nameLabel) {
        [self.nameLabel removeFromSuperview];
    }
    
}

///没有数据时进行相关的提示

- (UIImageView *)tipImage{
    if (!_tipImage) {
        _tipImage = [UIImageView new];
        //        _tipImage.image = XAImgName(imageName);
        _tipImage.frame = CGRectMake(0, 0, 90, 90);
        _tipImage.centerX = self.view.centerX;
        _tipImage.centerY = self.view.centerY-100;
    }
    return _tipImage;
}

-(UILabel *)nameLabel{
    
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.frame = CGRectMake(0, 0, 250, 18);
        _nameLabel.centerX = self.view.centerX;
        _nameLabel.centerY = self.tipImage.centerY + 60;
        //        _nameLabel.text = @"暂未收到消息";
        _nameLabel.textColor = [UIColor grayColor];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        //        _nameLabel.backgroundColor = [UIColor magentaColor];
    }
    return _nameLabel;
}
//获取当前时间
- (NSString *)getCurrentDate{
    NSDate *senddate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH-mm-ss"];
    NSString *locationString=[dateformatter stringFromDate:senddate];
    return locationString;
}
@end
